package com.qianying.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qianying.domain.DeleteParam;
import com.qianying.domain.TreeData;
import com.qianying.dto.*;
import com.qianying.entity.*;
import com.qianying.exception.BusinessException;
import com.qianying.mapper.*;
import com.qianying.service.OrderService;
import com.qianying.utils.DataHelper;
import com.qianying.utils.Dictionary;
import com.qianying.utils.TreeDataHandle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private MapMapper mapMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderRequirementsMapper orderRequirementsMapper;

    @Autowired
    private OrderRequirementsImgMapper orderRequirementsImgMapper;

    @Autowired
    private OrderRequirementsServiceMapper orderRequirementsServiceMapper;

    @Autowired
    private OrderSurveyQuotationMapper orderSurveyQuotationMapper;

    @Autowired
    private OrderSurveyQuotationImgMapper orderSurveyQuotationImgMapper;

    @Autowired
    private OrderSurveyQuotationPriceMapper orderSurveyQuotationPriceMapper;

    @Autowired
    private OrderPlatformPriceMapper orderPlatformPriceMapper;

    @Autowired
    private OrderPlatformPriceItemMapper orderPlatformPriceItemMapper;

    @Autowired
    private OrderDispatchMapper orderDispatchMapper;

    @Autowired
    private OrderRepairMapper orderRepairMapper;

    @Autowired
    private OrderRepairResultsImgMapper orderRepairResultsImgMapper;

    @Autowired
    private OrderRepairAcceptanceFormImgMapper orderRepairAcceptanceFormImgMapper;

    @Autowired
    private OrderRepairStoreImgMapper orderRepairStoreImgMapper;

    @Autowired
    private OrderRepairOnlineAcceptanceMapper orderRepairOnlineAcceptanceMapper;

    @Autowired
    private OrderRepairCustomerComplaintMapper orderRepairCustomerComplaintMapper;

    @Autowired
    private OrderFinanceMapper orderFinanceMapper;

    @Autowired
    private OrderFinanceChargeMapper orderFinanceChargeMapper;

    @Autowired
    private OrderFinanceOtherChargeMapper orderFinanceOtherChargeMapper;

    @Autowired
    private TreeDataHandle treeDataHandle;

    @Autowired
    private DataHelper dataHelper;

    private TreeData rootTree;

    public OrderServiceImpl(){

        this.rootTree = TreeData.builder()
                .tableName("`order`")
                .queryDataName("order")
                .isAutoOneRowToSubTable(true)
                .children(Arrays.asList(

                        TreeData.builder()
                                .tableName("order_requirements")
                                .queryDataName("requirements")
                                .filterQueryDataNameList(Arrays.asList(
                                        "orderId"
                                ))
                                .children(Arrays.asList(

                                        TreeData.builder()
                                                .tableName("order_requirements_img")
                                                .queryDataName("imgList")
                                                .filterQueryDataNameList(Arrays.asList(
                                                        "orderRequirementsId"
                                                ))
                                                .orderBy(" create_time   ")
                                                .build() ,

                                        TreeData.builder()
                                                .tableName("order_requirements_service")
                                                .queryDataName("serviceList")
                                                .filterQueryDataNameList(Arrays.asList(
                                                        "orderRequirementsId"
                                                ))
                                                .orderBy(" create_time  ")
                                                .build()
                                ))
                                .build(),

                        TreeData.builder()
                                .tableName("order_survey_quotation")
                                .queryDataName("surveyQuotation")
                                .filterQueryDataNameList(Arrays.asList(
                                        "orderId"
                                ))
                                .children(Arrays.asList(

                                        TreeData.builder()
                                                .tableName("order_survey_quotation_img")
                                                .queryDataName("imgList")
                                                .filterQueryDataNameList(Arrays.asList(
                                                        "orderSurveyQuotationId"
                                                ))
                                                .orderBy(" create_time  ")
                                                .build(),

                                        TreeData.builder()
                                                .tableName("order_survey_quotation_price")
                                                .queryDataName("priceList")
                                                .filterQueryDataNameList(Arrays.asList(
                                                        "orderSurveyQuotationId"
                                                ))
                                                .orderBy(" create_time  ")
                                                .build()
                                ))
                                .build(),

                        TreeData.builder()
                                .tableName("order_platform_price")
                                .queryDataName("platformPrice")
                                .filterQueryDataNameList(Arrays.asList(
                                        "orderId"
                                ))
                                .children(Arrays.asList(

                                        TreeData.builder()
                                                .tableName("order_platform_price_item")
                                                .queryDataName("priceList")
                                                .filterQueryDataNameList(Arrays.asList(
                                                        "orderPlatformPriceId"
                                                ))
                                                .orderBy(" create_time  ")
                                                .build()

                                ))
                                .build(),

                        TreeData.builder()
                                .tableName("order_dispatch")
                                .queryDataName("dispatch")
                                .filterQueryDataNameList(Arrays.asList(
                                        "orderId"
                                ))
                                .build(),

                        TreeData.builder()
                                .tableName("order_repair")
                                .queryDataName("repair")
                                .filterQueryDataNameList(Arrays.asList(
                                        "orderId"
                                ))
                                .children(Arrays.asList(

                                        TreeData.builder()
                                                .tableName("order_repair_results_img")
                                                .queryDataName("resultsImgList")
                                                .filterQueryDataNameList(Arrays.asList(
                                                        "orderRepairId"
                                                ))
                                                .orderBy(" create_time  ")
                                                .build(),

                                        TreeData.builder()
                                                .tableName("order_repair_acceptance_form_img")
                                                .queryDataName("acceptanceFormImgList")
                                                .filterQueryDataNameList(Arrays.asList(
                                                        "orderRepairId"
                                                ))
                                                .orderBy(" create_time  ")
                                                .build(),

                                        TreeData.builder()
                                                .tableName("order_repair_store_img")
                                                .queryDataName("storeImgList")
                                                .filterQueryDataNameList(Arrays.asList(
                                                        "orderRepairId"
                                                ))
                                                .orderBy(" create_time  ")
                                                .build(),

                                        TreeData.builder()
                                                .tableName("order_repair_online_acceptance")
                                                .queryDataName("onlineAcceptanceList")
                                                .filterQueryDataNameList(Arrays.asList(
                                                        "orderRepairId"
                                                ))
                                                .orderBy(" create_time  ")
                                                .build(),

                                        TreeData.builder()
                                                .tableName("order_repair_customer_complaint")
                                                .queryDataName("customerComplaintList")
                                                .filterQueryDataNameList(Arrays.asList(
                                                        "orderRepairId"
                                                ))
                                                .orderBy(" create_time  ")
                                                .build()
                                ))
                                .build(),

                        TreeData.builder()
                                .tableName("order_finance")
                                .queryDataName("finance")
                                .filterQueryDataNameList(Arrays.asList(
                                        "orderId"
                                ))
                                .children(Arrays.asList(

                                        TreeData.builder()
                                                .tableName("order_finance_charge")
                                                .queryDataName("chargeList")
                                                .filterQueryDataNameList(Arrays.asList(
                                                        "orderFinanceId"
                                                ))
                                                .orderBy(" create_time  ")
                                                .build(),

                                        TreeData.builder()
                                                .tableName("order_finance_other_charge")
                                                .queryDataName("otherChargeList")
                                                .filterQueryDataNameList(Arrays.asList(
                                                        "orderFinanceId"
                                                ))
                                                .orderBy(" create_time  ")
                                                .build()
                                ))
                                .build()
                ))
                .build();

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OrderDTO saveData(OrderDTO orderDTO) {

        if(orderDTO == null){
            throw new BusinessException(-2,"保存的数据不能空.");
        }

        orderHandleData( orderDTO );

        orderRequirementsHandleData( orderDTO.getRequirements() ,orderDTO.getId());

        orderSurveyQuotationHandleData( orderDTO.getSurveyQuotation(),orderDTO.getId() );

        orderPlatformPriceHandleData( orderDTO.getPlatformPrice(),orderDTO.getId() );

        orderDispatchHandleData( orderDTO.getDispatch(),orderDTO.getId() );

        orderRepairHandleData ( orderDTO.getRepair(),orderDTO.getId() );

        orderFinanceHandleData ( orderDTO.getFinance(),orderDTO.getId() );

        return orderDTO;
    }

    @Override
    public Map<String,Object> getData(OrderGetDataRequestDTO param) {

        Map<String,Object> whereCondition = new HashMap<>();

        if(param.getId() != null) {

            whereCondition.put(Dictionary.id,param.getId());
        }
        else {

            whereCondition.put("orderNo",param.getOrderNo());
        }

        this.rootTree.setWhereCondition(whereCondition);

        Map<String,Object> queryMap = new HashMap<>();

        treeDataHandle.queryDataTree(rootTree,queryMap);

        Map<String,Object> queryResultMap = new HashMap<>();

        if( queryMap.get(rootTree.getQueryDataName()) instanceof  Map){

            queryResultMap = (Map<String, Object>) queryMap.get(rootTree.getQueryDataName());
        }

        //如果是空数据是 (List< Map<String, Object> >) queryMap.get(rootTree.getQueryDataName());

        return queryResultMap ;

    }

    @Override
    public boolean deleteData(DeleteRequestByIdDTO deleteRequestByIdDTO) {

        treeDataHandle.deleteDataTree(this.rootTree,Dictionary.id,deleteRequestByIdDTO.getId());

        return true;
    }

    private void orderHandleData(OrderDTO dto){

        if(dto.getId() == null &&  dto.getOrderNo() == null ) {
            dto.setOrderNo( String.valueOf( System.currentTimeMillis() )  );
        }

        com.qianying.entity.Order entity = new Order();

        dataHelper.objCopyPropertiesThrow(dto, entity);

        if(dto.getId() == null) {

            orderMapper.insert(entity);

            //添加主键
            dto.setId(entity.getId());

            //默认添加
            if( dto.getRequirements() == null ){
                dto.setRequirements(new OrderRequirementsDTO());
            }

            if( dto.getSurveyQuotation() == null ){
                dto.setSurveyQuotation( new OrderSurveyQuotationDTO());
            }

            if( dto.getPlatformPrice() == null ){
                dto.setPlatformPrice(new OrderPlatformPriceDTO());
            }

            if( dto.getDispatch() == null ){
                dto.setDispatch(new OrderDispatchDTO());
            }

            if( dto.getRepair() == null ){
                dto.setRepair(new OrderRepairDTO());
            }

            if( dto.getFinance() == null ){
                dto.setFinance(new OrderFinanceDTO());
            }

        }else {

            if( canUpdate(entity) ) {

                orderMapper.updateById(entity);
            }

            // 特殊处理日期字段
            if( dto.isCreateOrderTimePresent() || dto.isStoreOpeningDatePresent() ){

                LambdaUpdateWrapper<Order> wrapper = new LambdaUpdateWrapper<Order>()
                        .eq(Order::getId, dto.getId());

                if (dto.isCreateOrderTimePresent()) {
                    wrapper.set(Order::getCreateOrderTime, dto.getCreateOrderTime());
                }

                if (dto.isStoreOpeningDatePresent()) {
                    wrapper.set(Order::getStoreOpeningDate, dto.getStoreOpeningDate());
                }

                orderMapper.update(wrapper);
            }

        }

    }

    private void orderRequirementsHandleData(OrderRequirementsDTO dto,long fkId){

        if(dto == null){
            return;
        }

        OrderRequirements entity = new OrderRequirements();

        dataHelper.objCopyPropertiesThrow(dto, entity);

        if(entity.getId() == null ){

            //设置外键
            entity.setOrderId(fkId);

            orderRequirementsMapper.insert(entity);

            //添加主键
            dto.setId(entity.getId());


        }
        else {

            if( canUpdate( entity ) ){

                //剔除外键
                entity.setOrderId(null);

                orderRequirementsMapper.updateById(entity);
            }

            // 特殊处理日期字段
            if( dto.isHopeRepairTimePresent()  ){

                LambdaUpdateWrapper<OrderRequirements> wrapper = new LambdaUpdateWrapper<OrderRequirements>()
                        .eq(OrderRequirements::getId, dto.getId());

                wrapper.set(OrderRequirements::getHopeRepairTime, dto.getHopeRepairTime());

                orderRequirementsMapper.update(wrapper);

            }

        }

        //处理子表

        orderRequirementsImgHandleData( dto.getImgList(),dto.getId() );

        orderRequirementsServiceHandleData( dto.getServiceList(),dto.getId()  );

    }

    private void orderRequirementsImgHandleData(List<OrderRequirementsImgDTO> dataList ,long fkId){

        if(dataList == null ){
            return;
        }

        orderRequirementsImgMapper.deleteByParentId(fkId);

        for (OrderRequirementsImgDTO item : dataList ){

            OrderRequirementsImg entity = new OrderRequirementsImg();

            dataHelper.objCopyPropertiesThrow(item, entity);

            entity.setId(null);
            entity.setSeq( dataList.indexOf(item) + 1 );

            //设置外键
            entity.setOrderRequirementsId(fkId);

            orderRequirementsImgMapper.insert(entity);

            //填充主键
            item.setId(entity.getId());

        }


//        for (OrderRequirementsImgDTO item : imgList ){
//
//            OrderRequirementsImg entity = new OrderRequirementsImg();
//
//            dataHelper.objCopyPropertiesThrow(item, entity);
//
//            if(entity.getId() == null ){
//
//                //设置外键
//                entity.setOrderRequirementsId(fkId);
//
//                orderRequirementsImgMapper.insert(entity);
//
//                //回填id
//                item.setId(entity.getId());
//
//            }
//            else {
//
//                if( canUpdate(entity) ){
//
//                    //剔除外键
//                    entity.setOrderRequirementsId(null);
//
//                    orderRequirementsImgMapper.updateById(entity);
//                }
//
//
//            }
//
//        }

    }

    private void orderRequirementsServiceHandleData(List<OrderRequirementsServiceDTO> dataList,long fkId){

        if(dataList == null){
            return;
        }

        orderRequirementsServiceMapper.deleteByParentId(fkId);

        for (OrderRequirementsServiceDTO item : dataList ){

            OrderRequirementsService entity = new OrderRequirementsService();

            dataHelper.objCopyPropertiesThrow(item, entity);

            entity.setId(null);
            entity.setSeq( dataList.indexOf(item) + 1 );

            //设置外键
            entity.setOrderRequirementsId(fkId);

            orderRequirementsServiceMapper.insert(entity);

            //回填id
            item.setId(entity.getId());

        }



//        for (OrderRequirementsServiceDTO item : serviceList ){
//
//            OrderRequirementsService entity = new OrderRequirementsService();
//
//            dataHelper.objCopyPropertiesThrow(item, entity);
//
//            if(entity.getId() == null ){
//
//                //设置外键
//                entity.setOrderRequirementsId(fkId);
//
//                orderRequirementsServiceMapper.insert(entity);
//
//                //回填id
//                item.setId(entity.getId());
//
//            }
//            else {
//
//                if(canUpdate(entity)){
//
//                    //剔除外键
//                    entity.setOrderRequirementsId(null);
//
//                    orderRequirementsServiceMapper.updateById(entity);
//                }
//
//            }
//
//        }

    }

    private void orderSurveyQuotationHandleData(OrderSurveyQuotationDTO dto,long fkId){

        if(dto == null){

            return;
        }

        OrderSurveyQuotation entity = new OrderSurveyQuotation();

        dataHelper.objCopyPropertiesThrow(dto, entity);

        if(entity.getId() == null ){

            entity.setOrderId(fkId);

            orderSurveyQuotationMapper.insert(entity);

            //回填id
            dto.setId(entity.getId());

        }
        else {

            if( canUpdate(entity) ){

                //剔除外键
                entity.setOrderId(null);

                orderSurveyQuotationMapper.updateById(entity);
            }

            // 特殊处理日期字段
            if( dto.isReservationTimePresent()  ){

                LambdaUpdateWrapper<OrderSurveyQuotation> wrapper = new LambdaUpdateWrapper<OrderSurveyQuotation>()
                        .eq(OrderSurveyQuotation::getId, dto.getId());

                wrapper.set(OrderSurveyQuotation::getReservationTime, dto.getReservationTime());

                orderSurveyQuotationMapper.update(wrapper);
            }

        }

        //处理子表

        orderSurveyQuotationImgHandleData( dto.getImgList() ,dto.getId() );

        orderSurveyQuotationPriceHandleData ( dto.getPriceList() ,dto.getId());
    }

    private void orderSurveyQuotationImgHandleData(List<OrderSurveyQuotationImgDTO> dataList ,long fkId){

        if( dataList  == null ){
            return;
        }

        orderSurveyQuotationImgMapper.deleteByParentId(fkId);

        for (OrderSurveyQuotationImgDTO item : dataList  ) {

            OrderSurveyQuotationImg entity = new OrderSurveyQuotationImg();

            dataHelper.objCopyPropertiesThrow(item, entity);

            entity.setId(null);
            entity.setSeq( dataList.indexOf(item) + 1 );
            entity.setOrderSurveyQuotationId(fkId);

            orderSurveyQuotationImgMapper.insert(entity);

            item.setId(entity.getId());
        }


//        for (OrderSurveyQuotationImgDTO item : dataList  ){
//
//            OrderSurveyQuotationImg entity = new OrderSurveyQuotationImg();
//
//            dataHelper.objCopyPropertiesThrow(item, entity);
//
//            if(entity.getId() == null ){
//
//                entity.setOrderSurveyQuotationId(fkId);
//
//                orderSurveyQuotationImgMapper.insert(entity);
//
//                //回填id
//                item.setId(entity.getId());
//            }
//            else {
//
//                if( canUpdate(entity)  ){
//
//                    //剔除外键
//                    entity.setOrderSurveyQuotationId(null);
//
//                    orderSurveyQuotationImgMapper.updateById(entity);
//                }
//            }
//        }

    }

    private void orderSurveyQuotationPriceHandleData(List<OrderSurveyQuotationPriceDTO> dataList,long fkId ){

        if( dataList  == null ){
            return;
        }

        orderSurveyQuotationPriceMapper.deleteByParentId(fkId);

        for (OrderSurveyQuotationPriceDTO item : dataList  ){

            OrderSurveyQuotationPrice entity = new OrderSurveyQuotationPrice();

            dataHelper.objCopyPropertiesThrow(item, entity);

            entity.setId(null);
            entity.setSeq( dataList.indexOf(item) + 1 );
            entity.setOrderSurveyQuotationId(fkId);

            orderSurveyQuotationPriceMapper.insert(entity);

            item.setId(entity.getId());

        }

//        for (OrderSurveyQuotationPriceDTO item : dataList  ){
//
//            OrderSurveyQuotationPrice entity = new OrderSurveyQuotationPrice();
//
//            dataHelper.objCopyPropertiesThrow(item, entity);
//
//            if(entity.getId() == null ){
//
//                entity.setOrderSurveyQuotationId(fkId);
//
//                orderSurveyQuotationPriceMapper.insert(entity);
//
//                //回填id
//                item.setId(entity.getId());
//            }
//            else {
//
//                if( canUpdate(entity) ){
//
//                    //剔除外键
//                    entity.setOrderSurveyQuotationId(null);
//
//                    orderSurveyQuotationPriceMapper.updateById(entity);
//                }
//
//            }
//        }

    }

    private void orderPlatformPriceHandleData(OrderPlatformPriceDTO dto,long fkId){

        if(dto == null){
            return;
        }

        OrderPlatformPrice entity = new OrderPlatformPrice();

        dataHelper.objCopyPropertiesThrow(dto, entity);

        if(entity.getId() == null ){

            entity.setOrderId(fkId);

            orderPlatformPriceMapper.insert(entity);

            //添加主键
            dto.setId(entity.getId());

        }
        else {

            if(canUpdate( entity )){

                //剔除外键
                entity.setOrderId(null);

                orderPlatformPriceMapper.updateById(entity);
            }

        }

        orderPlatformPriceItemHandleData( dto.getPriceList(), dto.getId() );
    }

    private void orderPlatformPriceItemHandleData( List<OrderPlatformPriceItemDTO> dataList,long fkId) {

        if( dataList  == null ){
            return;
        }

        orderPlatformPriceItemMapper.deleteByParentId(fkId);

        for (OrderPlatformPriceItemDTO item : dataList  ) {

            OrderPlatformPriceItem entity = new OrderPlatformPriceItem();

            dataHelper.objCopyPropertiesThrow(item, entity);

            entity.setId(null);
            entity.setSeq( dataList.indexOf(item) + 1 );

            entity.setOrderPlatformPriceId(fkId);

            orderPlatformPriceItemMapper.insert(entity);

            item.setId(entity.getId());
        }


//        for (OrderPlatformPriceItemDTO item : dataList  ){
//
//            OrderPlatformPriceItem entity = new OrderPlatformPriceItem();
//
//            dataHelper.objCopyPropertiesThrow(item, entity);
//
//            if(entity.getId() == null ){
//
//                entity.setOrderPlatformPriceId(fkId);
//
//                orderPlatformPriceItemMapper.insert(entity);
//
//                //回填id
//                item.setId(entity.getId());
//            }
//            else {
//
//                if(canUpdate( entity)) {
//
//                    //剔除外键
//                    entity.setOrderPlatformPriceId(null);
//
//                    orderPlatformPriceItemMapper.updateById(entity);
//                }
//            }
//        }
    }

    private void orderDispatchHandleData(OrderDispatchDTO dto ,long fkId){

        if(dto == null){
            return;
        }

        OrderDispatch entity = new OrderDispatch();

        dataHelper.objCopyPropertiesThrow(dto, entity);

        if(entity.getId() == null ){

            entity.setOrderId(fkId);

            orderDispatchMapper.insert(entity);

            //添加主键
            dto.setId(entity.getId());

        }
        else {


            if( canUpdate(entity) ){

                //剔除外键
                entity.setOrderId(null);

                orderDispatchMapper.updateById(entity);

            }

            // 特殊处理日期字段
            if( dto.isAppointmentTimePresent()  ){

                LambdaUpdateWrapper<OrderDispatch> wrapper = new LambdaUpdateWrapper<OrderDispatch>()
                        .eq(OrderDispatch::getId, dto.getId());

                wrapper.set(OrderDispatch::getAppointmentTime, dto.getAppointmentTime());

                orderDispatchMapper.update(wrapper);

            }

        }


    }

    private void orderRepairHandleData(OrderRepairDTO dto,long fkId) {

        if(dto == null){
            return;
        }

        OrderRepair entity = new OrderRepair();

        dataHelper.objCopyPropertiesThrow(dto, entity);

        if(entity.getId() == null ){

            entity.setOrderId(fkId);

            orderRepairMapper.insert(entity);

            //添加主键
            dto.setId(entity.getId());


        }
        else {

            if( canUpdate(entity) ){

                //剔除外键
                entity.setOrderId(null);

                orderRepairMapper.updateById(entity);

            }

            // 特殊处理日期字段
            if( dto.isDepartureTimePresent() || dto.isArrivalTimePresent() ||  dto.isCustomerAcceptanceTimePresent() || dto.isPlatformAcceptanceaTimePresent()  ){

                LambdaUpdateWrapper<OrderRepair> wrapper = new LambdaUpdateWrapper<OrderRepair>()
                        .eq(OrderRepair::getId, dto.getId());

                if( dto.isDepartureTimePresent() ){
                    wrapper.set(OrderRepair::getDepartureTime, dto.getDepartureTime());
                }

                if( dto.isArrivalTimePresent() ){
                    wrapper.set(OrderRepair::getArrivalTime, dto.getArrivalTime());
                }

                if( dto.isCustomerAcceptanceTimePresent() ){
                    wrapper.set(OrderRepair::getCustomerAcceptanceTime, dto.getCustomerAcceptanceTime());
                }

                if( dto.isPlatformAcceptanceaTimePresent() ){
                    wrapper.set(OrderRepair::getPlatformAcceptanceaTime, dto.getPlatformAcceptanceaTime());
                }

                orderRepairMapper.update(wrapper);

            }

        }

        //处理子表

        orderRepairResultsImgHandleData( dto.getResultsImgList(),dto.getId() );

        orderRepairAcceptanceFormImgHandleData( dto.getAcceptanceFormImgList(),dto.getId() );

        orderRepairStoreImgHandleData( dto.getStoreImgList(),dto.getId() );

        orderRepairOnlineAcceptanceHandleData( dto.getOnlineAcceptanceList(),dto.getId() );

        orderRepairCustomerComplaintHandleData( dto.getCustomerComplaintList(),dto.getId() );

    }

    private void orderRepairResultsImgHandleData( List<OrderRepairResultsImgDTO> dataList,long fkId ) {

        if( dataList  == null ){
            return;
        }

        orderRepairResultsImgMapper.deleteByParentId(fkId);

        for (OrderRepairResultsImgDTO item : dataList  ) {

            OrderRepairResultsImg entity = new OrderRepairResultsImg();

            dataHelper.objCopyPropertiesThrow(item, entity);

            entity.setId(null);
            entity.setSeq( dataList.indexOf(item) + 1 );

            entity.setOrderRepairId(fkId);

            orderRepairResultsImgMapper.insert(entity);

            //回填id
            item.setId(entity.getId());
        }


//        for (OrderRepairResultsImgDTO item : dataList  ){
//
//            OrderRepairResultsImg entity = new OrderRepairResultsImg();
//
//            dataHelper.objCopyPropertiesThrow(item, entity);
//
//            if(entity.getId() == null ){
//
//                entity.setOrderRepairId(fkId);
//
//                orderRepairResultsImgMapper.insert(entity);
//
//                //回填id
//                item.setId(entity.getId());
//            }
//            else {
//
//                if( canUpdate(entity)){
//
//                    //剔除外键
//                    entity.setOrderRepairId(null);
//
//                    orderRepairResultsImgMapper.updateById(entity);
//
//                }
//            }
//        }
    }

    private void orderRepairAcceptanceFormImgHandleData( List<OrderRepairAcceptanceFormImgDTO> dataList,long fkId ) {

        if( dataList  == null ){
            return;
        }

        orderRepairAcceptanceFormImgMapper.deleteByParentId(fkId);

        for (OrderRepairAcceptanceFormImgDTO item : dataList  ){

            OrderRepairAcceptanceFormImg entity = new OrderRepairAcceptanceFormImg();

            dataHelper.objCopyPropertiesThrow(item, entity);

            entity.setId(null);
            entity.setSeq( dataList.indexOf(item) + 1 );

            entity.setOrderRepairId(fkId);

            orderRepairAcceptanceFormImgMapper.insert(entity);

            //回填id
            item.setId(entity.getId());


        }


//        for (OrderRepairAcceptanceFormImgDTO item : dataList  ){
//
//            OrderRepairAcceptanceFormImg entity = new OrderRepairAcceptanceFormImg();
//
//            dataHelper.objCopyPropertiesThrow(item, entity);
//
//            if(entity.getId() == null ){
//
//                entity.setOrderRepairId(fkId);
//
//                orderRepairAcceptanceFormImgMapper.insert(entity);
//
//                //回填id
//                item.setId(entity.getId());
//            }
//            else {
//
//                if( canUpdate(entity)){
//
//                    //剔除外键
//                    entity.setOrderRepairId(null);
//
//                    orderRepairAcceptanceFormImgMapper.updateById(entity);
//
//                }
//
//            }
//        }
    }

    private void orderRepairStoreImgHandleData( List<OrderRepairStoreImgDTO> dataList ,long fkId) {

        if( dataList  == null ){
            return;
        }

        orderRepairStoreImgMapper.deleteByParentId(fkId);

        for (OrderRepairStoreImgDTO item : dataList  ) {

            OrderRepairStoreImg entity = new OrderRepairStoreImg();

            dataHelper.objCopyPropertiesThrow(item, entity);

            entity.setId(null);
            entity.setSeq( dataList.indexOf(item) + 1 );

            entity.setOrderRepairId(fkId);

            orderRepairStoreImgMapper.insert(entity);

            //回填id
            item.setId(entity.getId());

        }



//        for (OrderRepairStoreImgDTO item : dataList  ){
//
//            OrderRepairStoreImg entity = new OrderRepairStoreImg();
//
//            dataHelper.objCopyPropertiesThrow(item, entity);
//
//            if(entity.getId() == null ){
//
//                entity.setOrderRepairId(fkId);
//
//                orderRepairStoreImgMapper.insert(entity);
//
//                //回填id
//                item.setId(entity.getId());
//            }
//            else {
//
//                if( canUpdate(entity)){
//
//                    //剔除外键
//                    entity.setOrderRepairId(null);
//
//                    orderRepairStoreImgMapper.updateById(entity);
//                }
//
//            }
//        }
    }

    private void orderRepairOnlineAcceptanceHandleData( List<OrderRepairOnlineAcceptanceDTO> dataList,long fkId ) {

        if( dataList  == null ){
            return;
        }

        orderRepairOnlineAcceptanceMapper.deleteByParentId(fkId);

        for (OrderRepairOnlineAcceptanceDTO item : dataList  ){

            OrderRepairOnlineAcceptance entity = new OrderRepairOnlineAcceptance();

            dataHelper.objCopyPropertiesThrow(item, entity);

            entity.setId(null);
            entity.setSeq( dataList.indexOf(item) + 1 );

            entity.setOrderRepairId(fkId);

            orderRepairOnlineAcceptanceMapper.insert(entity);

            //回填id
            item.setId(entity.getId());

        }

//        for (OrderRepairOnlineAcceptanceDTO item : dataList  ){
//
//            OrderRepairOnlineAcceptance entity = new OrderRepairOnlineAcceptance();
//
//            dataHelper.objCopyPropertiesThrow(item, entity);
//
//            if(entity.getId() == null ){
//
//                entity.setOrderRepairId(fkId);
//
//                orderRepairOnlineAcceptanceMapper.insert(entity);
//
//                //回填id
//                item.setId(entity.getId());
//            }
//            else {
//
//                if( canUpdate(entity)){
//
//                    //剔除外键
//                    entity.setOrderRepairId(null);
//
//                    orderRepairOnlineAcceptanceMapper.updateById(entity);
//                }
//
//            }
//        }
    }

    private void orderRepairCustomerComplaintHandleData( List<OrderRepairCustomerComplaintDTO> dataList,long fkId ) {

        if( dataList  == null ){
            return;
        }

        orderRepairCustomerComplaintMapper.deleteByParentId(fkId);

        for (OrderRepairCustomerComplaintDTO item : dataList  ){

            OrderRepairCustomerComplaint entity = new OrderRepairCustomerComplaint();

            dataHelper.objCopyPropertiesThrow(item, entity);

            entity.setId(null);
            entity.setSeq( dataList.indexOf(item) + 1 );

            entity.setOrderRepairId(fkId);

            orderRepairCustomerComplaintMapper.insert(entity);

            //回填id
            item.setId(entity.getId());

        }

//        for (OrderRepairCustomerComplaintDTO item : dataList  ){
//
//            OrderRepairCustomerComplaint entity = new OrderRepairCustomerComplaint();
//
//            dataHelper.objCopyPropertiesThrow(item, entity);
//
//            if(entity.getId() == null ){
//
//                entity.setOrderRepairId(fkId);
//
//                orderRepairCustomerComplaintMapper.insert(entity);
//
//                //回填id
//                item.setId(entity.getId());
//            }
//            else {
//
//                if( canUpdate(entity)){
//
//                    //剔除外键
//                    entity.setOrderRepairId(null);
//
//                    orderRepairCustomerComplaintMapper.updateById(entity);
//                }
//
//            }
//        }
    }

    private void orderFinanceHandleData(OrderFinanceDTO dto,long fkId ){


        if(dto == null){
            return;
        }

        OrderFinance entity = new OrderFinance();

        dataHelper.objCopyPropertiesThrow(dto, entity);

        if(entity.getId() == null ){

            entity.setOrderId(fkId);

            orderFinanceMapper.insert(entity);

            //回填id
            dto.setId(entity.getId());


        }
        else {


            if( canUpdate(entity) ){

                //剔除外键
                entity.setOrderId(null);

                orderFinanceMapper.updateById(entity);

            }

            // 特殊处理日期字段
            if( dto.isInvoicingTimePresent() || dto.isPaymentRequestDatePresent() || dto.isPaymentDatePresent() ){

                LambdaUpdateWrapper<OrderFinance> wrapper = new LambdaUpdateWrapper<OrderFinance>()
                        .eq(OrderFinance::getId, dto.getId());

                if(dto.isInvoicingTimePresent()){
                    wrapper.set(OrderFinance::getInvoicingTime, dto.getInvoicingTime());

                }

                if(dto.isPaymentRequestDatePresent()){
                    wrapper.set(OrderFinance::getPaymentRequestDate, dto.getPaymentRequestDate());
                }

                if(dto.isPaymentDatePresent()){
                    wrapper.set(OrderFinance::getPaymentDate, dto.getPaymentDate());
                }

                orderFinanceMapper.update(wrapper);
            }

        }

        //处理子表

        orderFinanceChargeHandleData( dto.getChargeList() ,dto.getId() );

        orderFinanceOtherChargeHandleData ( dto.getOtherChargeList(),dto.getId() );

    }

    private void orderFinanceChargeHandleData( List<OrderFinanceChargeDTO> dataList ,long fkId ) {

        if( dataList  == null ){
            return;
        }

        orderFinanceChargeMapper.deleteByParentId( fkId );

        for (OrderFinanceChargeDTO item : dataList  ){

            OrderFinanceCharge entity = new OrderFinanceCharge();

            dataHelper.objCopyPropertiesThrow(item, entity);

            entity.setId(null);
            entity.setSeq( dataList.indexOf(item) + 1 );
            entity.setOrderFinanceId(fkId);

            orderFinanceChargeMapper.insert(entity);

            //回填id
            item.setId(entity.getId());

        }

//        for (OrderFinanceChargeDTO item : dataList  ){
//
//            OrderFinanceCharge entity = new OrderFinanceCharge();
//
//            dataHelper.objCopyPropertiesThrow(item, entity);
//
//            if(entity.getId() == null ){
//
//                entity.setOrderFinanceId(fkId);
//
//                orderFinanceChargeMapper.insert(entity);
//
//                //回填id
//                item.setId(entity.getId());
//            }
//            else {
//
//                if( canUpdate( entity ))
//                {
//                    //剔除外键
//                    entity.setOrderFinanceId(null);
//                    orderFinanceChargeMapper.updateById(entity);
//
//                }
//            }
//        }
    }

    private void orderFinanceOtherChargeHandleData( List<OrderFinanceOtherChargeDTO> dataList ,long fkId ) {

        if( dataList  == null ){
            return;
        }

        orderFinanceOtherChargeMapper.deleteByParentId( fkId );

        for (OrderFinanceOtherChargeDTO item : dataList  ){

            OrderFinanceOtherCharge entity = new OrderFinanceOtherCharge();

            dataHelper.objCopyPropertiesThrow(item, entity);

            entity.setId(null);
            entity.setSeq( dataList.indexOf(item) + 1 );
            entity.setOrderFinanceId(fkId);

            orderFinanceOtherChargeMapper.insert(entity);

            //回填id
            item.setId(entity.getId());
        }


//        for (OrderFinanceOtherChargeDTO item : dataList  ){
//
//            OrderFinanceOtherCharge entity = new OrderFinanceOtherCharge();
//
//            dataHelper.objCopyPropertiesThrow(item, entity);
//
//            if(entity.getId() == null ){
//
//                entity.setOrderFinanceId(fkId);
//
//                orderFinanceOtherChargeMapper.insert(entity);
//
//                //回填id
//                item.setId(entity.getId());
//            }
//            else {
//
//                if( canUpdate( entity ))
//                {
//                    //剔除外键
//                    entity.setOrderFinanceId(null);
//
//                    orderFinanceOtherChargeMapper.updateById(entity);
//                }
//
//            }
//        }
    }

    private boolean canUpdate(Object obj ){

        Map<String,Object> updateMap =  dataHelper.objToMapThrow(obj);

        //没有包含id
        if(updateMap.get(Dictionary.id) == null ){
            return false;
        }

        //只包含Id
        if(updateMap.get(Dictionary.id) != null && updateMap.size() == 1 ){
            return false;
        }

        return true;
    }

}
